package mapper

import (
	"encoding/csv"
	"fmt"
	"io"
	"log"
	"strings"
)

var zoneMap map[string]string

func init() {
	zoneMap = make(map[string]string)
	csvReader := csv.NewReader(strings.NewReader(mapping))

	for {
		record, err := csvReader.Read()
		if err == io.EOF {
			break
		}

		if err != nil {
			log.Println("[GoTimeZoneMapper.init] Failed to parse country to timzone mapping")
		}

		zoneMap[record[2]] = record[1]
	}
}

// FindCountryCodeForTimezone : Returns country-code in ISO 3166-1 alpha-2 format (Ex India - IN)
func FindCountryCodeForTimezone(timezone string) (string, error) {
	var countryCode string
	var found bool

	if countryCode, found = zoneMap[timezone]; !found {
		return "", fmt.Errorf("[GoTimeZoneMapper.FindCountryCodeForTimezone] Country-Code not found for %s", timezone)
	}

	return countryCode, nil
}

const mapping = `"1","AD","Europe/Andorra"
"2","AE","Asia/Dubai"
"3","AF","Asia/Kabul"
"4","AG","America/Antigua"
"5","AI","America/Anguilla"
"6","AL","Europe/Tirane"
"7","AM","Asia/Yerevan"
"8","AO","Africa/Luanda"
"9","AQ","Antarctica/Casey"
"10","AQ","Antarctica/Davis"
"11","AQ","Antarctica/DumontDUrville"
"12","AQ","Antarctica/Mawson"
"13","AQ","Antarctica/McMurdo"
"14","AQ","Antarctica/Palmer"
"15","AQ","Antarctica/Rothera"
"16","AQ","Antarctica/Syowa"
"17","AQ","Antarctica/Troll"
"18","AQ","Antarctica/Vostok"
"19","AR","America/Argentina/Buenos_Aires"
"20","AR","America/Argentina/Catamarca"
"21","AR","America/Argentina/Cordoba"
"22","AR","America/Argentina/Jujuy"
"23","AR","America/Argentina/La_Rioja"
"24","AR","America/Argentina/Mendoza"
"25","AR","America/Argentina/Rio_Gallegos"
"26","AR","America/Argentina/Salta"
"27","AR","America/Argentina/San_Juan"
"28","AR","America/Argentina/San_Luis"
"29","AR","America/Argentina/Tucuman"
"30","AR","America/Argentina/Ushuaia"
"31","AS","Pacific/Pago_Pago"
"32","AT","Europe/Vienna"
"33","AU","Antarctica/Macquarie"
"34","AU","Australia/Adelaide"
"35","AU","Australia/Brisbane"
"36","AU","Australia/Broken_Hill"
"37","AU","Australia/Currie"
"38","AU","Australia/Darwin"
"39","AU","Australia/Eucla"
"40","AU","Australia/Hobart"
"41","AU","Australia/Lindeman"
"42","AU","Australia/Lord_Howe"
"43","AU","Australia/Melbourne"
"44","AU","Australia/Perth"
"45","AU","Australia/Sydney"
"46","AW","America/Aruba"
"47","AX","Europe/Mariehamn"
"48","AZ","Asia/Baku"
"49","BA","Europe/Sarajevo"
"50","BB","America/Barbados"
"51","BD","Asia/Dhaka"
"52","BE","Europe/Brussels"
"53","BF","Africa/Ouagadougou"
"54","BG","Europe/Sofia"
"55","BH","Asia/Bahrain"
"56","BI","Africa/Bujumbura"
"57","BJ","Africa/Porto-Novo"
"58","BL","America/St_Barthelemy"
"59","BM","Atlantic/Bermuda"
"60","BN","Asia/Brunei"
"61","BO","America/La_Paz"
"62","BQ","America/Kralendijk"
"63","BR","America/Araguaina"
"64","BR","America/Bahia"
"65","BR","America/Belem"
"66","BR","America/Boa_Vista"
"67","BR","America/Campo_Grande"
"68","BR","America/Cuiaba"
"69","BR","America/Eirunepe"
"70","BR","America/Fortaleza"
"71","BR","America/Maceio"
"72","BR","America/Manaus"
"73","BR","America/Noronha"
"74","BR","America/Porto_Velho"
"75","BR","America/Recife"
"76","BR","America/Rio_Branco"
"77","BR","America/Santarem"
"78","BR","America/Sao_Paulo"
"79","BS","America/Nassau"
"80","BT","Asia/Thimphu"
"81","BW","Africa/Gaborone"
"82","BY","Europe/Minsk"
"83","BZ","America/Belize"
"84","CA","America/Atikokan"
"85","CA","America/Blanc-Sablon"
"86","CA","America/Cambridge_Bay"
"87","CA","America/Creston"
"88","CA","America/Dawson"
"89","CA","America/Dawson_Creek"
"90","CA","America/Edmonton"
"91","CA","America/Fort_Nelson"
"92","CA","America/Glace_Bay"
"93","CA","America/Goose_Bay"
"94","CA","America/Halifax"
"95","CA","America/Inuvik"
"96","CA","America/Iqaluit"
"97","CA","America/Moncton"
"98","CA","America/Nipigon"
"99","CA","America/Pangnirtung"
"100","CA","America/Rainy_River"
"101","CA","America/Rankin_Inlet"
"102","CA","America/Regina"
"103","CA","America/Resolute"
"104","CA","America/St_Johns"
"105","CA","America/Swift_Current"
"106","CA","America/Thunder_Bay"
"107","CA","America/Toronto"
"108","CA","America/Vancouver"
"109","CA","America/Whitehorse"
"110","CA","America/Winnipeg"
"111","CA","America/Yellowknife"
"112","CC","Indian/Cocos"
"113","CD","Africa/Kinshasa"
"114","CD","Africa/Lubumbashi"
"115","CF","Africa/Bangui"
"116","CG","Africa/Brazzaville"
"117","CH","Europe/Zurich"
"118","CI","Africa/Abidjan"
"119","CK","Pacific/Rarotonga"
"120","CL","America/Punta_Arenas"
"121","CL","America/Santiago"
"122","CL","Pacific/Easter"
"123","CM","Africa/Douala"
"124","CN","Asia/Shanghai"
"125","CN","Asia/Urumqi"
"126","CO","America/Bogota"
"127","CR","America/Costa_Rica"
"128","CU","America/Havana"
"129","CV","Atlantic/Cape_Verde"
"130","CW","America/Curacao"
"131","CX","Indian/Christmas"
"132","CY","Asia/Famagusta"
"133","CY","Asia/Nicosia"
"134","CZ","Europe/Prague"
"135","DE","Europe/Berlin"
"136","DE","Europe/Busingen"
"137","DJ","Africa/Djibouti"
"138","DK","Europe/Copenhagen"
"139","DM","America/Dominica"
"140","DO","America/Santo_Domingo"
"141","DZ","Africa/Algiers"
"142","EC","America/Guayaquil"
"143","EC","Pacific/Galapagos"
"144","EE","Europe/Tallinn"
"145","EG","Africa/Cairo"
"146","EH","Africa/El_Aaiun"
"147","ER","Africa/Asmara"
"148","ES","Africa/Ceuta"
"149","ES","Atlantic/Canary"
"150","ES","Europe/Madrid"
"151","ET","Africa/Addis_Ababa"
"152","FI","Europe/Helsinki"
"153","FJ","Pacific/Fiji"
"154","FK","Atlantic/Stanley"
"155","FM","Pacific/Chuuk"
"156","FM","Pacific/Kosrae"
"157","FM","Pacific/Pohnpei"
"158","FO","Atlantic/Faroe"
"159","FR","Europe/Paris"
"160","GA","Africa/Libreville"
"161","GB","Europe/London"
"162","GD","America/Grenada"
"163","GE","Asia/Tbilisi"
"164","GF","America/Cayenne"
"165","GG","Europe/Guernsey"
"166","GH","Africa/Accra"
"167","GI","Europe/Gibraltar"
"168","GL","America/Danmarkshavn"
"169","GL","America/Nuuk"
"170","GL","America/Scoresbysund"
"171","GL","America/Thule"
"172","GM","Africa/Banjul"
"173","GN","Africa/Conakry"
"174","GP","America/Guadeloupe"
"175","GQ","Africa/Malabo"
"176","GR","Europe/Athens"
"177","GS","Atlantic/South_Georgia"
"178","GT","America/Guatemala"
"179","GU","Pacific/Guam"
"180","GW","Africa/Bissau"
"181","GY","America/Guyana"
"182","HK","Asia/Hong_Kong"
"183","HN","America/Tegucigalpa"
"184","HR","Europe/Zagreb"
"185","HT","America/Port-au-Prince"
"186","HU","Europe/Budapest"
"187","ID","Asia/Jakarta"
"188","ID","Asia/Jayapura"
"189","ID","Asia/Makassar"
"190","ID","Asia/Pontianak"
"191","IE","Europe/Dublin"
"192","IL","Asia/Jerusalem"
"193","IM","Europe/Isle_of_Man"
"194","IN","Asia/Kolkata"
"195","IO","Indian/Chagos"
"196","IQ","Asia/Baghdad"
"197","IR","Asia/Tehran"
"198","IS","Atlantic/Reykjavik"
"199","IT","Europe/Rome"
"200","JE","Europe/Jersey"
"201","JM","America/Jamaica"
"202","JO","Asia/Amman"
"203","JP","Asia/Tokyo"
"204","KE","Africa/Nairobi"
"205","KG","Asia/Bishkek"
"206","KH","Asia/Phnom_Penh"
"207","KI","Pacific/Enderbury"
"208","KI","Pacific/Kiritimati"
"209","KI","Pacific/Tarawa"
"210","KM","Indian/Comoro"
"211","KN","America/St_Kitts"
"212","KP","Asia/Pyongyang"
"213","KR","Asia/Seoul"
"214","KW","Asia/Kuwait"
"215","KY","America/Cayman"
"216","KZ","Asia/Almaty"
"217","KZ","Asia/Aqtau"
"218","KZ","Asia/Aqtobe"
"219","KZ","Asia/Atyrau"
"220","KZ","Asia/Oral"
"221","KZ","Asia/Qostanay"
"222","KZ","Asia/Qyzylorda"
"223","LA","Asia/Vientiane"
"224","LB","Asia/Beirut"
"225","LC","America/St_Lucia"
"226","LI","Europe/Vaduz"
"227","LK","Asia/Colombo"
"228","LR","Africa/Monrovia"
"229","LS","Africa/Maseru"
"230","LT","Europe/Vilnius"
"231","LU","Europe/Luxembourg"
"232","LV","Europe/Riga"
"233","LY","Africa/Tripoli"
"234","MA","Africa/Casablanca"
"235","MC","Europe/Monaco"
"236","MD","Europe/Chisinau"
"237","ME","Europe/Podgorica"
"238","MF","America/Marigot"
"239","MG","Indian/Antananarivo"
"240","MH","Pacific/Kwajalein"
"241","MH","Pacific/Majuro"
"242","MK","Europe/Skopje"
"243","ML","Africa/Bamako"
"244","MM","Asia/Yangon"
"245","MN","Asia/Choibalsan"
"246","MN","Asia/Hovd"
"247","MN","Asia/Ulaanbaatar"
"248","MO","Asia/Macau"
"249","MP","Pacific/Saipan"
"250","MQ","America/Martinique"
"251","MR","Africa/Nouakchott"
"252","MS","America/Montserrat"
"253","MT","Europe/Malta"
"254","MU","Indian/Mauritius"
"255","MV","Indian/Maldives"
"256","MW","Africa/Blantyre"
"257","MX","America/Bahia_Banderas"
"258","MX","America/Cancun"
"259","MX","America/Chihuahua"
"260","MX","America/Hermosillo"
"261","MX","America/Matamoros"
"262","MX","America/Mazatlan"
"263","MX","America/Merida"
"264","MX","America/Mexico_City"
"265","MX","America/Monterrey"
"266","MX","America/Ojinaga"
"267","MX","America/Tijuana"
"268","MY","Asia/Kuala_Lumpur"
"269","MY","Asia/Kuching"
"270","MZ","Africa/Maputo"
"271","NA","Africa/Windhoek"
"272","NC","Pacific/Noumea"
"273","NE","Africa/Niamey"
"274","NF","Pacific/Norfolk"
"275","NG","Africa/Lagos"
"276","NI","America/Managua"
"277","NL","Europe/Amsterdam"
"278","NO","Europe/Oslo"
"279","NP","Asia/Kathmandu"
"280","NR","Pacific/Nauru"
"281","NU","Pacific/Niue"
"282","NZ","Pacific/Auckland"
"283","NZ","Pacific/Chatham"
"284","OM","Asia/Muscat"
"285","PA","America/Panama"
"286","PE","America/Lima"
"287","PF","Pacific/Gambier"
"288","PF","Pacific/Marquesas"
"289","PF","Pacific/Tahiti"
"290","PG","Pacific/Bougainville"
"291","PG","Pacific/Port_Moresby"
"292","PH","Asia/Manila"
"293","PK","Asia/Karachi"
"294","PL","Europe/Warsaw"
"295","PM","America/Miquelon"
"296","PN","Pacific/Pitcairn"
"297","PR","America/Puerto_Rico"
"298","PS","Asia/Gaza"
"299","PS","Asia/Hebron"
"300","PT","Atlantic/Azores"
"301","PT","Atlantic/Madeira"
"302","PT","Europe/Lisbon"
"303","PW","Pacific/Palau"
"304","PY","America/Asuncion"
"305","QA","Asia/Qatar"
"306","RE","Indian/Reunion"
"307","RO","Europe/Bucharest"
"308","RS","Europe/Belgrade"
"309","RU","Asia/Anadyr"
"310","RU","Asia/Barnaul"
"311","RU","Asia/Chita"
"312","RU","Asia/Irkutsk"
"313","RU","Asia/Kamchatka"
"314","RU","Asia/Khandyga"
"315","RU","Asia/Krasnoyarsk"
"316","RU","Asia/Magadan"
"317","RU","Asia/Novokuznetsk"
"318","RU","Asia/Novosibirsk"
"319","RU","Asia/Omsk"
"320","RU","Asia/Sakhalin"
"321","RU","Asia/Srednekolymsk"
"322","RU","Asia/Tomsk"
"323","RU","Asia/Ust-Nera"
"324","RU","Asia/Vladivostok"
"325","RU","Asia/Yakutsk"
"326","RU","Asia/Yekaterinburg"
"327","RU","Europe/Astrakhan"
"328","RU","Europe/Kaliningrad"
"329","RU","Europe/Kirov"
"330","RU","Europe/Moscow"
"331","RU","Europe/Samara"
"332","RU","Europe/Saratov"
"333","RU","Europe/Ulyanovsk"
"334","RU","Europe/Volgograd"
"335","RW","Africa/Kigali"
"336","SA","Asia/Riyadh"
"337","SB","Pacific/Guadalcanal"
"338","SC","Indian/Mahe"
"339","SD","Africa/Khartoum"
"340","SE","Europe/Stockholm"
"341","SG","Asia/Singapore"
"342","SH","Atlantic/St_Helena"
"343","SI","Europe/Ljubljana"
"344","SJ","Arctic/Longyearbyen"
"345","SK","Europe/Bratislava"
"346","SL","Africa/Freetown"
"347","SM","Europe/San_Marino"
"348","SN","Africa/Dakar"
"349","SO","Africa/Mogadishu"
"350","SR","America/Paramaribo"
"351","SS","Africa/Juba"
"352","ST","Africa/Sao_Tome"
"353","SV","America/El_Salvador"
"354","SX","America/Lower_Princes"
"355","SY","Asia/Damascus"
"356","SZ","Africa/Mbabane"
"357","TC","America/Grand_Turk"
"358","TD","Africa/Ndjamena"
"359","TF","Indian/Kerguelen"
"360","TG","Africa/Lome"
"361","TH","Asia/Bangkok"
"362","TJ","Asia/Dushanbe"
"363","TK","Pacific/Fakaofo"
"364","TL","Asia/Dili"
"365","TM","Asia/Ashgabat"
"366","TN","Africa/Tunis"
"367","TO","Pacific/Tongatapu"
"368","TR","Europe/Istanbul"
"369","TT","America/Port_of_Spain"
"370","TV","Pacific/Funafuti"
"371","TW","Asia/Taipei"
"372","TZ","Africa/Dar_es_Salaam"
"373","UA","Europe/Kiev"
"374","UA","Europe/Simferopol"
"375","UA","Europe/Uzhgorod"
"376","UA","Europe/Zaporozhye"
"377","UG","Africa/Kampala"
"378","UM","Pacific/Midway"
"379","UM","Pacific/Wake"
"380","US","America/Adak"
"381","US","America/Anchorage"
"382","US","America/Boise"
"383","US","America/Chicago"
"384","US","America/Denver"
"385","US","America/Detroit"
"386","US","America/Indiana/Indianapolis"
"387","US","America/Indiana/Knox"
"388","US","America/Indiana/Marengo"
"389","US","America/Indiana/Petersburg"
"390","US","America/Indiana/Tell_City"
"391","US","America/Indiana/Vevay"
"392","US","America/Indiana/Vincennes"
"393","US","America/Indiana/Winamac"
"394","US","America/Juneau"
"395","US","America/Kentucky/Louisville"
"396","US","America/Kentucky/Monticello"
"397","US","America/Los_Angeles"
"398","US","America/Menominee"
"399","US","America/Metlakatla"
"400","US","America/New_York"
"401","US","America/Nome"
"402","US","America/North_Dakota/Beulah"
"403","US","America/North_Dakota/Center"
"404","US","America/North_Dakota/New_Salem"
"405","US","America/Phoenix"
"406","US","America/Sitka"
"407","US","America/Yakutat"
"408","US","Pacific/Honolulu"
"409","UY","America/Montevideo"
"410","UZ","Asia/Samarkand"
"411","UZ","Asia/Tashkent"
"412","VA","Europe/Vatican"
"413","VC","America/St_Vincent"
"414","VE","America/Caracas"
"415","VG","America/Tortola"
"416","VI","America/St_Thomas"
"417","VN","Asia/Ho_Chi_Minh"
"418","VU","Pacific/Efate"
"419","WF","Pacific/Wallis"
"420","WS","Pacific/Apia"
"421","YE","Asia/Aden"
"422","YT","Indian/Mayotte"
"423","ZA","Africa/Johannesburg"
"424","ZM","Africa/Lusaka"
"425","ZW","Africa/Harare"
`
